/* Octopus PDF - A simple document viewer for Android.
   Copyright (C) 2013  Gerald Thaler

   This program is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation, either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>. */
package de.geraldthaler.octopuspdf.view;

import android.annotation.SuppressLint;
import android.opengl.GLSurfaceView;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.MotionEvent;
import de.geraldthaler.octopuspdf.MainActivity;
import de.geraldthaler.octopuspdf.docrender.DocSource;
import de.geraldthaler.octopuspdf.memory.Disposable;

/**
 * @author Gerald Thaler
 */
@SuppressLint("ViewConstructor")
public final class MainView extends GLSurfaceView implements Disposable {
	private final MainActivity activity;
    private final GestureDetector gestureDetector;
    private final GlRenderer glRenderer;
    
    public MainView(final MainActivity activity, final Bundle state, final DocSource docSource, final int pageMode) {
        super(activity);
        this.activity = activity;
        gestureDetector = new GestureDetector(activity, new GestureDetector.SimpleOnGestureListener() {
            @Override
            public boolean onFling(final MotionEvent e1, final MotionEvent e2, final float velocityX,
                    final float velocityY) {
                return MainView.this.onFling(e1, e2, velocityX, velocityY);
            }
            
            @Override
            public boolean onSingleTapUp(final MotionEvent e) {
            	return MainView.this.onSingleTapUp();
            }
        });
        setEGLConfigChooser(8, 8, 8, 8, 0, 0);
        glRenderer = new GlRenderer(activity, this, state, docSource, pageMode);
        setRenderer(glRenderer);
        setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY);
    }

	@Override
    public void dispose() {
        glRenderer.dispose(); 
    }

    @Override
    public boolean onTouchEvent(final MotionEvent event) {
        gestureDetector.onTouchEvent(event);
        final Touch touch = Touch.make(event);
        if (touch != null)
            glRenderer.sendEvent(touch);
        return true;
    }

    public void saveInstanceState(final Bundle state) {
        glRenderer.saveInstanceState(state);
    }

    public void sendEvent(final Object event) {
    	glRenderer.sendEvent(event);
    }

	boolean onFling(final MotionEvent e1, final MotionEvent e2, final float velocityX, final float velocityY) {
        glRenderer.sendEvent(new Fling(e1, e2, velocityX, velocityY));
        return true;
    }

    boolean onSingleTapUp() {
    	activity.onTap();
		return true;
	}
}
